package net.minecraft.block;

import com.mojang.serialization.MapCodec;
import java.util.List;
import java.util.function.Predicate;
import net.minecraft.block.enums.RailShape;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityCollisionHandler;
import net.minecraft.entity.vehicle.AbstractMinecartEntity;
import net.minecraft.entity.vehicle.CommandBlockMinecartEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.predicate.entity.EntityPredicates;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.EnumProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.state.property.Property;
import net.minecraft.util.BlockMirror;
import net.minecraft.util.BlockRotation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;

public class DetectorRailBlock extends AbstractRailBlock {
	public static final MapCodec<DetectorRailBlock> CODEC = createCodec(DetectorRailBlock::new);
	public static final EnumProperty<RailShape> SHAPE = Properties.STRAIGHT_RAIL_SHAPE;
	public static final BooleanProperty POWERED = Properties.POWERED;
	private static final int SCHEDULED_TICK_DELAY = 20;

	@Override
	public MapCodec<DetectorRailBlock> getCodec() {
		return CODEC;
	}

	public DetectorRailBlock(AbstractBlock.Settings settings) {
		super(true, settings);
		this.setDefaultState(this.stateManager.getDefaultState().with(POWERED, false).with(SHAPE, RailShape.NORTH_SOUTH).with(WATERLOGGED, false));
	}

	@Override
	protected boolean emitsRedstonePower(BlockState state) {
		return true;
	}

	@Override
	protected void onEntityCollision(BlockState state, World world, BlockPos pos, Entity entity, EntityCollisionHandler handler) {
		if (!world.isClient) {
			if (!(Boolean)state.get(POWERED)) {
				this.updatePoweredStatus(world, pos, state);
			}
		}
	}

	@Override
	protected void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
		if ((Boolean)state.get(POWERED)) {
			this.updatePoweredStatus(world, pos, state);
		}
	}

	@Override
	protected int getWeakRedstonePower(BlockState state, BlockView world, BlockPos pos, Direction direction) {
		return state.get(POWERED) ? 15 : 0;
	}

	@Override
	protected int getStrongRedstonePower(BlockState state, BlockView world, BlockPos pos, Direction direction) {
		if (!(Boolean)state.get(POWERED)) {
			return 0;
		} else {
			return direction == Direction.UP ? 15 : 0;
		}
	}

	private void updatePoweredStatus(World world, BlockPos pos, BlockState state) {
		if (this.canPlaceAt(state, world, pos)) {
			boolean bl = (Boolean)state.get(POWERED);
			boolean bl2 = false;
			List<AbstractMinecartEntity> list = this.getCarts(world, pos, AbstractMinecartEntity.class, entity -> true);
			if (!list.isEmpty()) {
				bl2 = true;
			}

			if (bl2 && !bl) {
				BlockState blockState = state.with(POWERED, true);
				world.setBlockState(pos, blockState, Block.NOTIFY_ALL);
				this.updateNearbyRails(world, pos, blockState, true);
				world.updateNeighbors(pos, this);
				world.updateNeighbors(pos.down(), this);
				world.scheduleBlockRerenderIfNeeded(pos, state, blockState);
			}

			if (!bl2 && bl) {
				BlockState blockState = state.with(POWERED, false);
				world.setBlockState(pos, blockState, Block.NOTIFY_ALL);
				this.updateNearbyRails(world, pos, blockState, false);
				world.updateNeighbors(pos, this);
				world.updateNeighbors(pos.down(), this);
				world.scheduleBlockRerenderIfNeeded(pos, state, blockState);
			}

			if (bl2) {
				world.scheduleBlockTick(pos, this, 20);
			}

			world.updateComparators(pos, this);
		}
	}

	protected void updateNearbyRails(World world, BlockPos pos, BlockState state, boolean unpowering) {
		RailPlacementHelper railPlacementHelper = new RailPlacementHelper(world, pos, state);

		for (BlockPos blockPos : railPlacementHelper.getNeighbors()) {
			BlockState blockState = world.getBlockState(blockPos);
			world.updateNeighbor(blockState, blockPos, blockState.getBlock(), null, false);
		}
	}

	@Override
	protected void onBlockAdded(BlockState state, World world, BlockPos pos, BlockState oldState, boolean notify) {
		if (!oldState.isOf(state.getBlock())) {
			BlockState blockState = this.updateCurves(state, world, pos, notify);
			this.updatePoweredStatus(world, pos, blockState);
		}
	}

	@Override
	public Property<RailShape> getShapeProperty() {
		return SHAPE;
	}

	@Override
	protected boolean hasComparatorOutput(BlockState state) {
		return true;
	}

	@Override
	protected int getComparatorOutput(BlockState state, World world, BlockPos pos) {
		if ((Boolean)state.get(POWERED)) {
			List<CommandBlockMinecartEntity> list = this.getCarts(world, pos, CommandBlockMinecartEntity.class, cart -> true);
			if (!list.isEmpty()) {
				return ((CommandBlockMinecartEntity)list.get(0)).getCommandExecutor().getSuccessCount();
			}

			List<AbstractMinecartEntity> list2 = this.getCarts(world, pos, AbstractMinecartEntity.class, EntityPredicates.VALID_INVENTORIES);
			if (!list2.isEmpty()) {
				return ScreenHandler.calculateComparatorOutput((Inventory)list2.get(0));
			}
		}

		return 0;
	}

	private <T extends AbstractMinecartEntity> List<T> getCarts(World world, BlockPos pos, Class<T> entityClass, Predicate<Entity> entityPredicate) {
		return world.getEntitiesByClass(entityClass, this.getCartDetectionBox(pos), entityPredicate);
	}

	private Box getCartDetectionBox(BlockPos pos) {
		double d = 0.2;
		return new Box(pos.getX() + 0.2, pos.getY(), pos.getZ() + 0.2, pos.getX() + 1 - 0.2, pos.getY() + 1 - 0.2, pos.getZ() + 1 - 0.2);
	}

	@Override
	protected BlockState rotate(BlockState state, BlockRotation rotation) {
		RailShape railShape = state.get(SHAPE);
		RailShape railShape2 = this.rotateShape(railShape, rotation);
		return state.with(SHAPE, railShape2);
	}

	@Override
	protected BlockState mirror(BlockState state, BlockMirror mirror) {
		RailShape railShape = state.get(SHAPE);
		RailShape railShape2 = this.mirrorShape(railShape, mirror);
		return state.with(SHAPE, railShape2);
	}

	@Override
	protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
		builder.add(SHAPE, POWERED, WATERLOGGED);
	}
}
